{extend name="layout:base" /}

{block name="content"}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="test-table-reload-btn" style="margin-bottom: 10px;">
                <div class="layui-inline">
                    <button id="btnAdd" class="layui-btn layui-btn-sm layuiadmin-btn-list" data-type="add">添加用户</button>
                </div>
                <div class="layui-inline">
                    <input class="layui-input" name="keywords" id="keywords" autocomplete="off">
                </div>
                <button class="layui-btn" data-type="reload">搜索</button>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="tbList" class="layui-table" lay-filter="tbList" ></table>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container"></div>
            </script>
            <script type="text/html" id="state">
                <input type="checkbox" name="state" value="{{d.id}}" lay-skin="switch" lay-text="正常|锁定" lay-filter="state" {{ d.state== 1 ? 'checked' : '' }}
            </script>
            <script type="text/html" id="xs">
                {{#  if(d.xs_id== 0){ }}
                    <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="edit_xs" lay-text="分配销售-{{d.username}}" lay-tips="分配销售-{{d.username}}">分配</a>
                {{#  } else { }}
                    <a class="layui-btn layui-btn-xs {{#  if(d.xs_state== 2){ }} layui-btn-danger {{#  } }}" lay-event="edit_xs" lay-text="重新分配-{{d.username}}" lay-tips="分配销售-{{d.username}}">{{d.xs_name}}</a>
                {{#  } }}
            </script>
            <script type="text/html" id="op">
                <a class="layui-btn layui-btn-xs" lay-event="user_money" lay-tips="充值">充</a>
                <a class="layui-btn layui-btn-xs" lay-event="user_money" lay-tips="充值记录">记录</a>

                <a class="layui-btn layui-btn-xs" lay-event="edit" lay-tips="编辑客户-{{d.name}}">编辑</a>

                {{#  if(d.state== 1){ }}
                <a class="layui-btn layui-btn-xs layui-btn-primary" href="{:url('/admin/login/login_member')}?id={{d.id}}" target="_blank" lay-tips="登录后台-{{d.username}}">登</a>
                {{#  } }}
            </script>
        </div>
    </div>
</div>

{/block}

{block name="script"}
<script>
    layui.config({
        base: '__STATIC__/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'util','form'], function () {

        //表格
        var $ = layui.$,form = layui.form,layer = layui.layer,table = layui.table;
        table.render({
            elem: '#tbList',
            url: "{:url('getList')}",
            where:{usertype:'zc'},
            toolbar: '#toolbarDemo',
            cellMinWidth: 80,
            cols: [[
                {field: 'id', fixed: 'left', title: 'ID',width:66},
                {field: 'username',fixed: 'left',title: '用户名'},
                {field: 'create_time',  title: '注册时间'},
                {field: 'xs_name',  title: '销售',width:80, templet: '#xs'},
                {field: 'web_num',  title: '站数'},
                {field: 'webkey_num',  title: '词数'},
                {field: 'up_num',  title: '当日达标'},
                {field: 'up_num',  title: '当日消费'},
                {field: 'up_num',  title: '当月消费'},
                {field: 'up_num',  title: '上月消费'},
                {field: 'up_num',  title: '账户余额'},
                {field: 'up_num',  title: '日报表'},
                {field: 'state',fixed: 'right',  title: '状态',width:93, templet: '#state'},
                {fixed: 'right', title: '操作',width:180, toolbar: '#op'}
                // {field: 'money',fixed: 'left',title: '余额',width:120,templet:function (d) {return Number(d.money).toFixed(0);}},
                // {field: 'money_sc',title: '首冲',width:120,templet:function (d) {return Number(d.money_sc).toFixed(0);}},
                // {field: 'webNum',  title: '网站/关键词',width:120,templet:function (d) {return d.webNum+'/'+d.keyNum}},
                // {field: 'moneypay',  title: '消费',width:100,templet:function (d) {return Number(d.moneypay).toFixed(0);}},
            ]],
            page: true,
            limit:15
        });

        // 监听工具条
        table.on('tool(tbList)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var url = "{:url('edit')}?id=" + data['id'];
                var index = layer.open({
                    type: 2,
                    title: '客户资料修改',
                    area: ['90%', '90%'],
                    shade: 0,
                    maxmin: true,
                    content: url,
                    zIndex: layer.zIndex
                });
            }else if (obj.event === 'user_money') {
                var url = "{:url('/admin/money/user')}?userID=" + data['id'];
                var index = layer.open({
                    type: 2,
                    title: '用户资金',
                    area: ['90%', '90%'],
                    shade: 0,
                    maxmin: true,
                    content: url,
                    zIndex: layer.zIndex
                });
            }else if (obj.event === 'edit_xs') {
                var url = "{:url('edit_xs')}?id=" + data['id'];
                var index = layer.open({
                    type: 2,
                    title: '分配销售',
                    area: ['90%', '90%'],
                    shade: 0,
                    maxmin: true,
                    content: url,
                    zIndex: layer.zIndex
                });
            }
        });
        //监听单元格编辑
        table.on('edit(tbList)', function (obj) {
            var value = obj.value; //得到修改后的值
            var data = obj.data; //得到所在行所有键值
            var field = obj.field; //得到字段
            layer.msg('[ID: ' + data.id + '] ' + field + ' 字段更改为：' + value);

            $.post("{:url('edit')}", {id: data.id, content: value}, function (result) {
                if (result['code'] == 0) {
                    layer.msg(result['msg'], {icon: 1, time: 1000}, function () {

                    });
                } else {
                    layer.msg(result['msg'], {icon: 3, time: 3000});
                }
            });
        });


        //监听推荐
        form.on('switch(state)', function (data) {
            var id=data.value;
            layer.open({
                content: '确认将该客户停用(该客户下的所有站点都将停用)?'
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    $.post("{:url('stop_user')}",{id:id}, function (result) {
                        if (result['code'] == 0) {
                            layer.msg(result['msg'], {icon: 1, time: 2000}, function () {
                                location.reload();
                            });
                        } else {
                            layer.msg(result['msg'], {icon: 3, time: 3000});
                        }
                    });
                    form.render();
                    layer.close(index);
                }
                ,btn2: function(index, layero){
                    layer.close(index);
                }
            });
            return false;



        });


        //操作
        var $ = layui.$, active = {
            add: function () {
                layer.open({
                    type: 2,
                    title: '添加内容',
                    area: ['800px', '600px'],
                    shade: 0,
                    maxmin: true,
                    content: "{:url('add')}",
                    zIndex: layer.zIndex
                });
            },
            reload: function(){
                var istop=$('#istop').val();
                var order=$('#order').val();
                var cid=$('#cid').val();
                var keywords=$('#keywords').val();

                //执行重载
                table.reload('tbList', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        istop: istop
                        ,cid: cid
                        ,order: order
                        ,keywords: keywords
                    }
                });

            }
        };

         $('.test-table-reload-btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });

</script>
{/block}